package servlet;

import dao.MysqlConfiguration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

@WebServlet(name = "connectServlet", value = "/connect")
public class ConnectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过类调用它的静态连接方法
        Connection conn = MysqlConfiguration.MysqlConnection();
        if(conn != null){
            //连接成功
            request.setAttribute("message","数据库连接成功!");
        }else {
            //连接失败
            request.setAttribute("message","数据库连接失败!");
        }
        request.getRequestDispatcher("index.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
